import redis
pool = redis.ConnectionPool(host='192.168.209.200', port=6379, db=0, password=None,health_check_interval=30)
r = redis.Redis(connection_pool=pool)
#列表类型
r.lpush("list1","apple","banana","cherry")
r.lpush("list1","orange")
#按列显示所有元素
for i in r.lrange("list1",0,-1):
    print(i)
#向有序集合插入值
r.zadd("sset1",{"apple":1,"banana":10,"cherry":50})
print(r.zcard("sset1")) # 显示有序集合的元素个数
#以数组方式显示所有元素和分值
print("sset1:",r.zrange("sset1", 0, -1, withscores=True))
#哈希表类型
r.delete("hash1")
r.hset("hash1","name","red")
#新版redis废弃hmset，会有告警，建议改用hset
#r.hmset("hash1",{"color":"red","price":2.5,"place":"shangdong qingdao"})
r.hset("hash1", mapping={"color": "red", "price": 2.5, "place": "shangdong qingdao"})
print(r.hkeys("hash1")) # 取hash中所有的key
print(r.hget("hash1", "name"))    # 单个取hash的key对应的值
print(r.hmget("hash1", "name", "color","price","place")) # 多个取hash的key对应的值
for key in r.hscan_iter("hash1"):
    print(key)

r.close()
pool.close()